/**
 * Copyright (c) 2022 www.talkweb.com.cn Inc. AlL rights reserved.
 */
package com.sky.wsp.mybatis.plus.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.wsp.mybatis.plus.dao.UserMapper;
import com.sky.wsp.mybatis.plus.dao.entity.UserEntity;
import com.sky.wsp.mybatis.plus.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 用户表 Service服务实现类
 *
 * @author wangshaopeng@talkweb.com.cn
 * @Date 2023-01-11
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

//    @Resource
//    private UserMapper userMapper;

    @Override
    @Transactional(readOnly = true)
    public Page<UserEntity> findPage(UserEntity query, Page<UserEntity> page) {
        return super.page(page, getQueryWrapper(query));
    }

    @Override
    @Transactional(readOnly = true)
    public List<UserEntity> findList(UserEntity query) {
        return super.list(getQueryWrapper(query));
    }

    @Override
    public UserEntity getByMobile(String mobile) {
        UserEntity query = new UserEntity();
        query.setMobile(mobile);
        return super.getOne(getQueryWrapper(query));
    }

    private Wrapper getQueryWrapper(UserEntity query) {
        if (null == query) {
            return null;
        }
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
        // TODO 下面的查询条件，请按实际情况修改
        if (null != query.getTenantId()) {
            wrapper.eq(UserEntity::getTenantId, query.getTenantId());
        }
        if (null != query.getOrgId()) {
            wrapper.eq(UserEntity::getOrgId, query.getOrgId());
        }
        if (null != query.getName()) {
            wrapper.eq(UserEntity::getName, query.getName());
        }
        if (null != query.getAge()) {
            wrapper.eq(UserEntity::getAge, query.getAge());
        }
        return wrapper;
    }

    @Override
    public boolean delete(Long id) {
        // 物理删除
        return super.removeById(id);
    }
}
